package cn.com.ntr.jhclient.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import cn.com.ntr.jhclient.model.City;
import cn.com.ntr.jhclient.model.Province;
import cn.com.ntr.jhclient.model.ProvinceCity;

public class DBAllProvince implements DBInterface<ProvinceCity> {
	private DatabaseHelper helper;

	public DBAllProvince(Context context) {
		helper = new DatabaseHelper(context);
	}

	@Override
	public long insert(ProvinceCity all) {
		// TODO Auto-generated method stub
		ContentValues values = new ContentValues();
		values.put(ProvinceCity.PID, all.getpId());
		values.put(ProvinceCity.P_NAME, all.getpName());
		values.put(ProvinceCity.P_ENNAME, all.geteName());
		values.put(ProvinceCity.P_FID, all.getfId());
		values.put(ProvinceCity.P_ORDER, all.getOrderId());
		return helper.insertData(CreateDbConstants.TABLE_ALL_PROVINCE, values);
	}

	@Override
	public List<ProvinceCity> queryAll() {
		// TODO Auto-generated method stub
		return null;
	}

	public List<Province> queryProvince() {
		List<Province> list = new ArrayList<Province>();
		SQLiteDatabase db = helper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select * from "
				+ CreateDbConstants.TABLE_ALL_PROVINCE + " where "
				+ ProvinceCity.P_FID + "=?", new String[] { "0" });
		while (cursor.moveToNext()) {
			Province obj = new Province();
			obj.setProId(cursor.getString(cursor
					.getColumnIndex(ProvinceCity.PID)));
			obj.setProName(cursor.getString(cursor
					.getColumnIndex(ProvinceCity.P_NAME)));
			list.add(obj);
		}
		return list;
	}

	public List<City> queryCity(String pid) {
		List<City> list = new ArrayList<City>();
		SQLiteDatabase db = helper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select * from "
				+ CreateDbConstants.TABLE_ALL_PROVINCE + " where "
				+ ProvinceCity.P_FID + "=?", new String[] { pid });
		while (cursor.moveToNext()) {
			City obj = new City();
			obj.setCityName(cursor.getString(cursor
					.getColumnIndex(ProvinceCity.P_NAME)));
			obj.setCityId(cursor.getString(cursor
					.getColumnIndex(ProvinceCity.PID)));
			list.add(obj);
		}
		return list;
	}
	public int getCityCount(String pid){
		SQLiteDatabase db = helper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select * from "
				+ CreateDbConstants.TABLE_ALL_PROVINCE + " where "
				+ ProvinceCity.P_FID + "=?", new String[] { pid });
		return cursor.getCount();
	}
	
	public String getCityCodeByName(String cityName){
		SQLiteDatabase db = helper.getWritableDatabase();
		String code="";
		Cursor cursor = db.rawQuery("select p_id from "
				+ CreateDbConstants.TABLE_ALL_PROVINCE + " where "
				+ ProvinceCity.P_NAME + "=?", new String[] { cityName });
		while (cursor.moveToNext()) {
			code=cursor.getString(cursor.getColumnIndex(ProvinceCity.PID));
		}
		return code;
	}

}
